package com.gdhz.xiaohengqin.device.web;

import com.gdhz.xiaohengqin.device.model.entity.AccessGate;
import com.gdhz.xiaohengqin.device.model.entity.DoorInfo;
import com.gdhz.xiaohengqin.device.service.AccessGateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author Swallows
 * @date 2025-06-20 14:48
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/access-gates")
public class AccessGateController {

    private final AccessGateService accessGateService;

    @PostMapping("/save")
    public Mono<Void> saveAccessGate(@RequestBody Mono<AccessGate> accessGate) {

        return accessGate.flatMap(accessGateService::saveAccessGate);
    }

    @PutMapping("/{gateSn}/doors")
    public Mono<Void> saveDoors(@PathVariable Integer gateSn,
                                @RequestBody Mono<List<DoorInfo>> door) {
        return door.flatMap(doorInfos -> accessGateService.saveDoors(gateSn, doorInfos));
    }

    @GetMapping("list")
    public Flux<AccessGate> getAccessGates() {
        return accessGateService.findAll();
    }
}
